package com.woniuxy.yoga.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.pojo.Club2CoachVo;
import com.woniuxy.yoga.pojo.Clubcoach;
import com.woniuxy.yoga.pojo.CoachVo;
import com.woniuxy.yoga.service.ClubcoachService;
import com.woniuxy.yoga.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YChi
 * @since 2020-10-30
 */
@CrossOrigin
@RestController
@RequestMapping("/yoga/clubcoach")
public class ClubcoachController {

    @Autowired
    private ClubcoachService clubcoachService;

    @ApiOperation(value = "俱乐部添加教练")
    @GetMapping(value = "/saveClubCoach")
    public R saveClubCoach(Clubcoach clubcoach){
        boolean age = clubcoachService.save(clubcoach);

        if (age) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "俱乐部下的教练")
    @GetMapping(value = "/Club2Coach")
    public R getCoachByPage(@ApiParam(value = "当前页")@RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
                            @ApiParam(value = "每页的记录数")@RequestParam(value = "limit",required = true,defaultValue = "6") Integer limit,
                            Club2CoachVo club2CoachVo){
        //创建Page对象
        Page pageBean = new Page<>(page,limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(club2CoachVo.getCname())){
            queryWrapper.like("cname",club2CoachVo.getCname());
        }
        if(club2CoachVo.getClubid() != null){
            queryWrapper.like("clubid",club2CoachVo.getClubid());
        }
        Page resultPage = clubcoachService.page(pageBean, queryWrapper);

        //获取集合
        List records = resultPage.getRecords();

        //获取总记录数
        long total = resultPage.getTotal();
        if(records!=null){
            return R.ok().data("records",records).data("total",total);
        }else{
            return R.error().message("没有查到集合数据");
        }

    }
}

